package com.vegetable.modules.controller.baseData;


import com.alibaba.fastjson.JSON;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.ObjectUtil;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.baseData.BaseDict;
import com.vegetable.modules.entity.baseData.StallsClauses;
import com.vegetable.modules.service.baseData.IBaseDictService;
import com.vegetable.modules.service.baseData.IStallsClausesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 客商-费用明细表 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-07
 */
@Slf4j
@RestController
@RequestMapping("/stallsClauses")
@AllArgsConstructor
@Api(value = "客商的费用明细", tags = "客商的费用明细")
public class StallsClausesController extends AbstractController {

    private IBaseDictService baseDictService;
    private IStallsClausesService stallsClausesService;
    /**
     * 所有客商的费用明细列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "条件查询客商的费用明细列表", notes = "条件查询客商的费用明细列表")
    public R list(@RequestParam Map<String, Object> params){
        if (ObjectUtil.isEmpty(params.get("stallsId"))){
            return R.fail("客商的主键不能为空");
        }
        log.info("查询客商下的费用明细列表{}", JSON.toJSONString(params));
        PageUtils page = stallsClausesService.queryPage(params);

        return R.data( page);
    }

    /**
     * 新增客商的费用明细
     */
    @SysLog(value = "新增客商的费用明细")
    @PostMapping("/save")
    @ApiOperation(value = "新增客商的费用明细", notes = "新增客商的费用明细")
    public R save(@RequestBody StallsClauses stallsClauses){
        if (stallsClauses.getStallsId() == null){
            return R.fail("客商的主键不能为空");
        }
        BaseDict byId = baseDictService.getById(stallsClauses.getClauseId());
        if (byId == null){
            return R.fail("包装费用科目不存在");
        }
        stallsClauses.setClauseName(byId.getValueOne());
        stallsClausesService.save(stallsClauses);
        return R.status(true);
    }

    /**
     * 修改客商的费用明细
     */
    @SysLog(value = "修改客商的费用明细")
    @PostMapping("/update")
    @ApiOperation(value = "修改客商的费用明细", notes = "修改客商的费用明细")
    public R update(@RequestBody StallsClauses stallsClauses){
        BaseDict byId = baseDictService.getById(stallsClauses.getClauseId());
        if (byId == null){
            return R.fail("包装费用科目不存在");
        }
        stallsClauses.setClauseName(byId.getValueOne());
        stallsClausesService.updateById(stallsClauses);
        return R.status(true);
    }

    /**
     * 删除客商的费用明细
     */
    @SysLog(value = "删除客商的费用明细")
    @GetMapping("/delete")
    @ApiOperation(value = "根据客商的费用明细id删除客商的费用明细", notes = "根据客商的费用明细id删除客商的费用明细")
    public R delete(Long id){
        stallsClausesService.removeById(id);
        return R.status(true);
    }
}
